package Ast;

import dataStructure.Function;
import org.eclipse.jdt.core.dom.*;

import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class MethodVisitor extends ASTVisitor {
    List<Function> list;
    String classname;
    public MethodVisitor(List<Function> list){
        this.list = list;
    }

    @Override
    public boolean visit(FieldDeclaration node) {
        for (Object obj: node.fragments()) {
            VariableDeclarationFragment v = (VariableDeclarationFragment)obj;
//            System.out.println("Field:\t" + v.getName());
        }

        return true;
    }

    @Override
    public boolean visit(MethodDeclaration node) {
//        System.out.println("Method:\t" + node.getName());
        String str1 = String.valueOf(node.getBody());
//        System.out.println(str1);
//        System.out.println("--------");
        int linenum = CountNum(str1);
        String str3 = String.valueOf(node.getName());
        Function fc = new Function(this.classname,str1,linenum,str3);
        list.add(fc);

        return true;
    }

    //统计方法中TextRank有效的行数
    public static int CountNum(String str){
        int result = 0;
        Scanner sc = new Scanner(str);
        while (sc.hasNextLine()){
            String temp = sc.nextLine();
            boolean bo = Pattern.compile("(?i)[a-z]").matcher(temp).find();   //正则表达式匹配判断是否含有小写字母
            if (bo){
                result++;
            }
        }
        return result;
    }


    @Override
    public boolean visit(TypeDeclaration node) {
//        System.out.println("Class:\t" + node.getName());
        this.classname = String.valueOf(node.getName());
        return true;
    }


}
